home *** CD-ROM | disk | FTP | other *** search
/ PhotoSpin 7: Medical Elements / PhotoSpin 7: Medical Elements - Disc 1.iso / pc / Kick Me / Japanese / ar32j30a.exe / 1033 / FILE / ARCHIVE.Z / ACROBAT / PLUG_INS / AFORM.JS < prev    next >
Text File  |  1997-11-20  |  24KB  |  893 lines

  1. /*
  2.     ==========================================================================
  3.     Module: AForm.js
  4.     ==========================================================================
  5.     Pre-canned functions to map the user interface into JavaScripts.
  6.     ==========================================================================
  7.     The Software, including this file, is subject ot the End User License
  8.     Agreement.
  9.     Copyright (c) 1997, Adobe Systems Incorporated, All Rights Reserved.
  10.     ==========================================================================
  11. */
  12.  
  13. console.println("Acrobat Forms Built-in Functions Version 3.5");
  14.  
  15. RE_NUMBER_ENTRY_DOT_SEP = new Array(
  16.     "[+-]?\\d*\\.?\\d*"
  17. );
  18. RE_NUMBER_COMMIT_DOT_SEP = new Array(
  19.     "[+-]?\\d+(\\.\\d+)?",        /* -1.0 or -1 */
  20.     "[+-]?\\.\\d+",                /* -.1 */
  21.     "[+-]?\\d+\\."                /* -1. */
  22. );
  23. RE_NUMBER_ENTRY_COMMA_SEP = new Array(
  24.     "[+-]?\\d*,?\\d*"
  25. );
  26. RE_NUMBER_COMMIT_COMMA_SEP = new Array(
  27.     "[+-]?\\d+([.,]\\d+)?",        /* -1,0 or -1 */
  28.     "[+-]?[.,]\\d+",                /* -,1 */
  29.     "[+-]?\\d+[.,]"                /* -1, */
  30. );
  31. RE_ZIP_ENTRY = new Array(
  32.     "\\d{0,5}"
  33. );
  34. RE_ZIP_COMMIT = new Array(
  35.     "\\d{5}"
  36. );
  37. RE_ZIP4_ENTRY = new Array(
  38.     "\\d{0,5}(\\.|[- ])?\\d{0,4}"
  39. );
  40. RE_ZIP4_COMMIT = new Array(
  41.     "\\d{5}(\\.|[- ])?\\d{4}"
  42. );
  43. RE_PHONE_ENTRY = new Array(
  44.     "\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",        /* 555-1234 or 408 555-1234 */
  45.     "\\(\\d{0,3}",                                            /* (408 */
  46.     "\\(\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",    /* (408) 555-1234 */
  47.         /* (allow the addition of parens as an afterthought) */
  48.     "\\(\\d{0,3}(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",    /* (408 555-1234 */
  49.     "\\d{0,3}\\)(\\.|[- ])?\\d{0,3}(\\.|[- ])?\\d{0,4}",    /* 408) 555-1234 */
  50.     "011(\\.|[- \\d])*"                                        /* international */
  51. );
  52. RE_PHONE_COMMIT = new Array(
  53.     "\\d{3}(\\.|[- ])?\\d{4}",                            /* 555-1234 */
  54.     "\\d{3}(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}",            /* 408 555-1234 */
  55.     "\\(\\d{3}\\)(\\.|[- ])?\\d{3}(\\.|[- ])?\\d{4}",    /* (408) 555-1234 */
  56.     "011(\\.|[- \\d])*"                                    /* international */
  57. );
  58. RE_SSN_ENTRY = new Array(
  59.     "\\d{0,3}(\\.|[- ])?\\d{0,2}(\\.|[- ])?\\d{0,4}"
  60. );
  61. RE_SSN_COMMIT = new Array(
  62.     "\\d{3}(\\.|[- ])?\\d{2}(\\.|[- ])?\\d{4}"
  63. );
  64.  
  65. /* ==== Convenience Objects ==== */
  66.  
  67. /* Stock color definitions for ease of use. */
  68. color = new Object();
  69. color.transparent = new Array("T");
  70. color.black = new Array("G", 0);
  71. color.white = new Array("G", 1);
  72. color.red = new Array("RGB", 1, 0, 0);
  73. color.green = new Array("RGB", 0, 1, 0);
  74. color.blue = new Array("RGB", 0, 0, 1);
  75. color.cyan = new Array("CMYK", 1, 0, 0, 0);
  76. color.magenta = new Array("CMYK", 0, 1, 0, 0);
  77. color.yellow = new Array("CMYK", 0, 0, 1, 0);
  78.  
  79. /* Font definitions for ease of use */
  80. font = new Object();
  81. font.Times = "Times-Roman";
  82. font.TimesB = "Times-Bold";
  83. font.TimesI = "Times-Italic";
  84. font.TimesBI = "Times-BoldItalic";
  85. font.Helv = "Helvetica";
  86. font.HelvB = "Helvetica-Bold";
  87. font.HelvI = "Helvetica-Oblique";
  88. font.HelvBI = "Helvetica-BoldOblique";
  89. font.Cour = "Courier";
  90. font.CourB = "Courier-Bold";
  91. font.CourI = "Courier-Oblique";
  92. font.CourBI = "Courier-BoldOblique";
  93. font.Symbol = "Symbol";
  94. font.ZapfD = "ZapfDingbats";
  95.  
  96. /* Border style definitions for ease of use */
  97. border = new Object();
  98. border.s = "solid";
  99. border.d = "dashed";
  100. border.b = "beveled";
  101. border.i = "inset";
  102. border.u = "underline";
  103.  
  104. /* Radio/Check button style definitions for ease of use */
  105. style = new Object();
  106. style.ch = "check";
  107. style.cr = "cross";
  108. style.di = "diamond";
  109. style.ci = "circle";
  110. style.st = "star";
  111. style.sq = "square"; 
  112.  
  113. /* highlight modes of on a push button */
  114. highlight = new Object();
  115. highlight.n = "none";
  116. highlight.i = "invert";
  117. highlight.p = "push";
  118. highlight.o = "outline";
  119.  
  120. /* zoom types for a document */
  121. zoomtype = new Object();
  122. zoomtype.none = "NoVary";
  123. zoomtype.fitW = "FitWidth";
  124. zoomtype.fitH = "FitHeight";
  125. zoomtype.fitP = "FitPage";
  126. zoomtype.fitV = "FitVisibleWidth"
  127.  
  128. /* ==== Functions ==== */
  129.  
  130. /* these may be used a lot -- they are language independent */
  131.  
  132. AFDigitsRegExp = new RegExp();
  133. AFDigitsRegExp.compile("\\d+");
  134. AFPMRegExp = new RegExp();
  135. AFPMRegExp.compile(IDS_PM, "i");
  136. AFAMRegExp = new RegExp();
  137. AFAMRegExp.compile(IDS_AM, "i");
  138. AFTimeLongRegExp = new RegExp();
  139. AFTimeLongRegExp.compile("\\d{1,2}:\\d{1,2}:\\d{1,2}");
  140. AFTimeShortRegExp = new RegExp();
  141. AFTimeShortRegExp.compile("\\d{1,2}:\\d{1,2}");
  142.  
  143. function AFBuildRegExps(array)
  144. /* Takes an array of strings and turns it into an array of compiled regular
  145.  * expressions -- is used for the definitions that follow */
  146. {
  147.     var retVal = new Array();
  148.  
  149.     retVal.length = array.length;
  150.     for(var it = 0; it < array.length; it++)
  151.     {
  152.         retVal[it] = new RegExp();
  153.         retVal[it].compile(array[it], "i");
  154.     }
  155.     return retVal;
  156. }
  157.  
  158. /* these may be used a lot -- they are NOT language independent and are 
  159.  * derived from the localizable (RE_xxx) stuff above */
  160.  
  161. AFNumberDotSepEntryRegExp = AFBuildRegExps(RE_NUMBER_ENTRY_DOT_SEP);
  162. AFNumberDotSepCommitRegExp = AFBuildRegExps(RE_NUMBER_COMMIT_DOT_SEP);
  163. AFNumberCommaSepEntryRegExp = AFBuildRegExps(RE_NUMBER_ENTRY_COMMA_SEP);
  164. AFNumberCommaSepCommitRegExp = AFBuildRegExps(RE_NUMBER_COMMIT_COMMA_SEP);
  165. AFZipEntryRegExp = AFBuildRegExps(RE_ZIP_ENTRY);
  166. AFZipCommitRegExp = AFBuildRegExps(RE_ZIP_COMMIT);
  167. AFZip4EntryRegExp = AFBuildRegExps(RE_ZIP4_ENTRY);
  168. AFZip4CommitRegExp = AFBuildRegExps(RE_ZIP4_COMMIT);
  169. AFPhoneEntryRegExp = AFBuildRegExps(RE_PHONE_ENTRY);
  170. AFPhoneCommitRegExp = AFBuildRegExps(RE_PHONE_COMMIT);
  171. AFSSNEntryRegExp = AFBuildRegExps(RE_SSN_ENTRY);
  172. AFSSNCommitRegExp = AFBuildRegExps(RE_SSN_COMMIT);
  173. AFMonthsRegExp = AFBuildRegExps(IDS_MONTH_INFO.split(/\[\d+\]/));
  174.  
  175. function AFExactMatch(rePatterns, sString)
  176. {    /* match a string against an array of RegExps */
  177.     var it;
  178.  
  179.     if(!rePatterns.length && rePatterns.test(sString) && RegExp.lastMatch == sString)
  180.         return true;
  181.     for(it = 0; it < rePatterns.length; it++)
  182.         if(rePatterns[it].test(sString) && RegExp.lastMatch == sString)
  183.             return it + 1;
  184.     return 0;
  185. }
  186.  
  187. function AFExtractNums(string)
  188. {    /* returns an array of numbers that it managed to extract from the given 
  189.      * string or null on failure */
  190.     var nums = new Array();
  191.  
  192.     if (string.charAt(0) == '.' || string.charAt(0) == ',')
  193.         string = "0" + string;
  194.          
  195.     while(AFDigitsRegExp.test(string)) {
  196.         nums.length++;
  197.         nums[nums.length - 1] = RegExp.lastMatch;
  198.         string = RegExp.rightContext;
  199.     }
  200.     if(nums.length >= 1) return nums;
  201.     return null;
  202. }
  203.  
  204. function AFMakeNumber(string)
  205. {    /* attempts to make a number out of a string that may not use '.' as the
  206.      * seperator; it expects that the number is fairly well-behaved other than
  207.      * possibly having a non-JavaScript friendly separator */
  208.     var type = typeof string;
  209.  
  210.     if (type == "number")
  211.         return string;
  212.     if (type != "string")
  213.         return null;
  214.  
  215.     var array = AFExtractNums(string);
  216.  
  217.     if(array)
  218.         return array.join(".") * (string.indexOf("-") >= 0 ? -1.0 : 1.0);
  219.     else
  220.         return null;
  221. }
  222.  
  223. function AFExtractRegExp(rePattern, string)
  224. {    /* attempts to match the pattern given against the string given; on 
  225.      * success, returns an array containing (at index 0) the initial
  226.      * string with the matched text removed and (at index 1) the matched
  227.      * text; on failure, returns null */
  228.     var retVal = new Array();
  229.  
  230.     if(rePattern.test(string))
  231.     {
  232.         retVal.length = 2;
  233.         retVal[0] = RegExp.leftContext + RegExp.rightContext;
  234.         retVal[1] = RegExp.lastMatch;
  235.         return retVal;
  236.     }
  237.     return null;
  238. }
  239.  
  240. function AFExtractTime(string)
  241. {    /* attempts to extract a WELL FORMED time from a string; returned 
  242.      * is an array in the same vein as AFExtractRegExp or null on
  243.      * failure. a WELL FORMED time looks like 12:23:56pm */
  244.     
  245.     var pm = "";
  246.     var info;
  247.  
  248.     info = AFExtractRegExp(AFPMRegExp, string);
  249.     if(info)
  250.     {
  251.         pm = info[1];
  252.         string = info[0];
  253.     }
  254.     info = AFExtractRegExp(AFAMRegExp, string);
  255.     if(info)
  256.         string = info[0];
  257.     info = AFExtractRegExp(AFTimeLongRegExp, string);
  258.     if(info)
  259.     {
  260.         info[1] += pm;
  261.         return info;
  262.     }
  263.     info = AFExtractRegExp(AFTimeShortRegExp, string);
  264.     if(info)
  265.     {
  266.         info[1] += pm;
  267.         return info;
  268.     }
  269.     return null;
  270. }
  271.  
  272. function AFDateGetUTC(date)
  273. {    /* returns the utc for a date object */
  274.     return Date.UTC(date.getYear(), date.getMonth(), date.getDate(),
  275.         date.getHours(), date.getMinutes(), date.getSeconds());
  276. }
  277.  
  278. function AFGetMonthIndex(string)
  279. {    /* attempts to identify the given string as a month or a valid abbreviation,
  280.      * it expects the given string to be the valid month from the matced RegExp.
  281.      * returns the month index (January = 1) or zero on failure */
  282.     var monthre = new RegExp(string + "\\[(\\d+)\\]", "i");
  283.     var result = monthre.exec(IDS_MONTH_INFO);
  284.     
  285.     if(string && result) return 1.0 * result[1];
  286.     return 0;
  287. }
  288.  
  289. function AFMatchMonth(string)
  290. {    /* attempts to find a valid month embedded in a string; returns the month
  291.      * index (January = 1) or zero on failure */
  292.  
  293.     for(var it = 0; it < AFMonthsRegExp.length; it++)
  294.         if(AFMonthsRegExp[it].test(string))
  295.             return AFGetMonthIndex(RegExp.lastMatch);
  296.     return 0;
  297. }
  298.  
  299. function AFGetMonthString(index)
  300. {    /* returns the string corresponding to the given month or a string that
  301.      * is indicative of the fact that the index was invalid */
  302.     var monthre = new RegExp("(\\w+)\\[" + index + "\\]");
  303.     var result = monthre.exec(IDS_MONTH_INFO);
  304.  
  305.     if(result) return result[1];
  306.     return IDS_INVALID_MONTH;
  307. }
  308.  
  309. function AFParseTime(string, date)
  310. {    /* attempts to parse a string containing a time; returns null on failure
  311.      * or a Date object on success. Time can be in ugly format. */
  312.     var pm;
  313.     var nums = AFExtractNums(string);
  314.     if (!date)
  315.         date = new Date();
  316.     var hour;
  317.  
  318.     if(!string) return date;
  319.     if(!nums) return null;
  320.     if(nums.length < 2 || nums.length > 3) return null;
  321.     if(AFPMRegExp.test(string)) pm = true;
  322.     else pm = false;
  323.     hour = new Number(nums[0]); /* force it to number */
  324.     if(pm)
  325.     {
  326.         if(hour != 12) hour += 12;
  327.     }
  328.     else
  329.     {
  330.         if(hour == 12) hour = 0;
  331.     }
  332.     date.setHours(hour);
  333.     date.setMinutes(nums[1]);
  334.     if(nums.length == 3) date.setSeconds(nums[2]);
  335.     else date.setSeconds(0);
  336.     return date;
  337. }
  338.  
  339. function AFParseDate(string, longEntry, shortEntry, wordMonthEntry, monthYearEntry)
  340. {    /* attempts to parse a string containing some form of date; returns null
  341.      * on failure or a Date object on success */
  342.     var nums;
  343.     var year, month;
  344.     var date;
  345.     var info = AFExtractTime(string);
  346.  
  347.     if(!string) return new Date();
  348.  
  349.     if(info)
  350.         string = info[0];
  351.  
  352.     date = new Date();
  353.     nums = AFExtractNums(string);
  354.     if(!nums) return null;
  355.     if(nums.length == 3)
  356.     {
  357.         year = nums[eval(longEntry.charAt(0))];
  358.         date.setYear(year);
  359.         date.setMonth(nums[eval(longEntry.charAt(1))] - 1);
  360.         date.setDate(nums[eval(longEntry.charAt(2))]);
  361.         if (info)
  362.             AFParseTime(info[1], date);
  363.         return date;
  364.     }
  365.     month = AFMatchMonth(string);
  366.     if(nums.length == 2)
  367.     {
  368.         if(month)
  369.         {
  370.             year = nums[eval(wordMonthEntry.charAt(0))];
  371.             date.setYear(year);
  372.             date.setMonth(month - 1);
  373.             date.setDate(nums[eval(wordMonthEntry.charAt(1))]);
  374.             if (info)
  375.                 AFParseTime(info[1], date);
  376.             return date;
  377.         }
  378.         if(monthYearEntry)
  379.         {
  380.             date.setDate(1);
  381.             year = nums[eval(monthYearEntry.charAt(0))];
  382.             date.setYear(year);
  383.             date.setMonth(nums[eval(monthYearEntry.charAt(1))] - 1);
  384.         }
  385.         else
  386.         {
  387.             date.setMonth(nums[eval(shortEntry.charAt(0))] - 1);
  388.             date.setDate(nums[eval(shortEntry.charAt(1))]);
  389.         }
  390.         if (info)
  391.             AFParseTime(info[1], date);
  392.         return date;
  393.     }
  394.     if(month && nums.length == 1)
  395.     {
  396.         if(monthYearEntry)
  397.         {
  398.             year = nums[0];
  399.             date.setDate(1);
  400.             date.setMonth(month - 1);
  401.             date.setYear(year);
  402.         }
  403.         else
  404.         {
  405.             date.setMonth(month - 1);
  406.             date.setDate(nums[0]);
  407.         }
  408.         if (info)
  409.             AFParseTime(info[1], date);
  410.         return date;
  411.     }
  412.  
  413.     return null;
  414. }
  415.  
  416. function AFParseDateWithPDF(value, pdf)
  417. /* Intelligently parses a string for a date specification using the format for
  418.  * hints */
  419. {
  420.     switch (pdf) {
  421.     
  422.         case 0:
  423.         case 1:
  424.         case 2:
  425.         case 4:
  426.         case 5:
  427.         case 6:
  428.         case 10:
  429.         case 11:
  430.         case 12:
  431.         case 13:
  432.             return AFParseDate(value, "201", "01", "10", "");    /* standard: ymd, md, yd */
  433.             break;
  434.         case 3:
  435.         case 8:
  436.         case 9:
  437.             return AFParseDate(value, "201", "", "10", "10");    /* month / year only */
  438.             break;
  439.         case 7:
  440.             return AFParseDate(value, "012", "01", "10", "");    /* backwards: y-m-d */
  441.             break;
  442.     }
  443.     return null;
  444. }
  445.  
  446. function AFMergeChange(event)
  447. {    /* merges the last change with the uncomitted change */
  448.     var prefix, postfix;
  449.     var value = event.value;
  450.  
  451.     if(event.willCommit) return event.value;
  452.     if(event.selStart >= 0)
  453.         prefix = value.substring(0, event.selStart);
  454.     else prefix = "";
  455.     if(event.selEnd >= 0 && event.selEnd <= value.length)
  456.         postfix = value.substring(event.selEnd, value.length);
  457.     else postfix = "";
  458.     return prefix + event.change + postfix;
  459. }
  460.  
  461. function AFRange_Validate(bGreaterThan, nGreaterThan, bLessThan, nLessThan)
  462. {       /* This function validates the current event to ensure that its value is 
  463.     ** within the specified range. */
  464.     var cError = "";
  465.  
  466.     if (bGreaterThan && bLessThan) {
  467.         if (event.value < nGreaterThan || event.value > nLessThan)
  468.             cError = util.printf(IDS_GT_AND_LT, nGreaterThan, nLessThan);
  469.     } else if (bGreaterThan) {
  470.         if (event.value < nGreaterThan)
  471.             cError = util.printf(IDS_GREATER_THAN, nGreaterThan);
  472.     } else if (bLessThan) {
  473.         if (event.value > nLessThan)
  474.             cError = util.printf(IDS_LESS_THAN, nLessThan);
  475.     }
  476.     
  477.     if (cError != "") {
  478.         app.alert(cError, 0);
  479.         event.rc = false;
  480.     }
  481. }
  482.  
  483. function AFSimpleInit(cFunction)
  484. {    /* Convenience function used by AFSimple_Calculate. */
  485.     switch (cFunction)
  486.     {
  487.         case "PRD":
  488.             return 1.0;
  489.             break;
  490.         case "MIN":
  491.             return Number.MAX_VALUE;
  492.             break;
  493.         case "MAX":
  494.             return Number.MIN_VALUE;
  495.             break;
  496.     }
  497.  
  498.     return 0.0;
  499. }
  500.  
  501. function AFSimple(cFunction, nValue1, nValue2)
  502. {    /* Convenience function used by AFSimple_Calculate. */
  503.     var nValue = 1.0 * nValue1;
  504.  
  505.     /* Have to do this otherwise JavaScript thinks it's dealing with strings. */
  506.     nValue1 = 1.0 * nValue1;
  507.     nValue2 = 1.0 * nValue2;
  508.  
  509.     switch (cFunction)
  510.     {
  511.         case "AVG":
  512.         case "SUM":
  513.             nValue = nValue1 + nValue2;
  514.             break;
  515.         case "PRD":
  516.             nValue = nValue1 * nValue2;
  517.             break;
  518.         case "MIN":
  519.             if (nValue2 < nValue1)
  520.                 nValue = nValue2;
  521.             break;
  522.         case "MAX":
  523.             if (nValue2 > nValue1)
  524.                 nValue = nValue2;
  525.             break;
  526.     }
  527.  
  528.     return nValue;
  529. }
  530.  
  531. function AFSimple_Calculate(cFunction, cFields)
  532. {   /* Calculates the sum, average, product, etc. of the listed field values. */
  533.     var cField = "", nFields = 0, i, j, f, a;
  534.     var nValue = AFSimpleInit(cFunction);
  535.     var nTemp;
  536.  
  537.     /* Loop over the fields string extracting the user specified fields. */
  538.     for (i = 0; i < cFields.length; i++) {
  539.         var c = cFields.charAt(i);
  540.  
  541.         if (c == ",") {
  542.             /* Found a field, process it's value. */
  543.             f = this.getField(cField);
  544.  
  545.             a = f.getArray();
  546.             for (var j = 0; j < a.length; j++) {
  547.                 nTemp = AFMakeNumber(a[j].value);
  548.                 nValue = AFSimple(cFunction, nValue, nTemp);
  549.                 nFields++;
  550.             }
  551.             cField = "";
  552.  
  553.             /* Skip any white space after the comma. */
  554.             while (i + 1 < cFields.length && cFields.charAt(i + 1) == " ") {
  555.                 i++;
  556.             }
  557.         } else {        
  558.             /* Build up the field name. */
  559.             cField += c;
  560.         }
  561.     }
  562.     
  563.     /* Don't forget to process the last field, if any. */
  564.     if (cField != "") {
  565.         f = this.getField(cField);
  566.  
  567.         var a = f.getArray();
  568.  
  569.         for (var j = 0; j < a.length; j++) {
  570.             nTemp = AFMakeNumber(a[j].value);
  571.             nValue = AFSimple(cFunction, nValue, nTemp);
  572.             nFields++;
  573.         }
  574.     }
  575.  
  576.     if (cFunction == "AVG" && nFields > 0)
  577.         nValue /= nFields;
  578.  
  579.     event.value = nValue;
  580. }
  581.  
  582. function AFNumber_Keystroke(nDec, sepStyle, negStyle, currStyle, strCurrency, bCurrencyPrepend)
  583. {       /* This function validates the current keystroke event to make sure the
  584.         key pressed is reasonable for a numeric field. */
  585.  
  586.     var value = AFMergeChange(event);
  587.     var commit, noCommit;
  588.  
  589.     if(!value) return;
  590.     if(sepStyle > 1)
  591.     {
  592.         commit = AFNumberCommaSepCommitRegExp;
  593.         noCommit = AFNumberCommaSepEntryRegExp;
  594.     }
  595.     else
  596.     {
  597.         commit = AFNumberDotSepCommitRegExp;
  598.         noCommit = AFNumberDotSepEntryRegExp;
  599.     }
  600.     if(!AFExactMatch(event.willCommit ? commit : noCommit, value))
  601.     {
  602.         app.beep(0);
  603.         event.rc = false;
  604.     }
  605. }
  606.  
  607. function AFPercent_Keystroke(nDec, sepStyle)
  608. {
  609.         AFNumber_Keystroke(nDec, sepStyle, 0, 0, "", true);
  610. }
  611.  
  612. function AFSpecial_Keystroke(psf)
  613. {       /* This function validates the current keystroke event to make sure the
  614.         key pressed is reasonable for a "special" field. */
  615.         
  616.     /* The special formats, indicated by psf, are:
  617.     
  618.     psf             format
  619.     ---             ------
  620.     0               zip code
  621.     1               zip + 4
  622.     2               phone
  623.     3                SSN
  624.     
  625.     */
  626.  
  627.     var value = AFMergeChange(event);
  628.     var commit, noCommit;
  629.  
  630.     if(!value) return;
  631.     switch (psf)
  632.     {
  633.         case 0:
  634.             commit = AFZipCommitRegExp;
  635.             noCommit = AFZipEntryRegExp;
  636.             break;
  637.         case 1:
  638.             commit = AFZip4CommitRegExp;
  639.             noCommit = AFZip4EntryRegExp;
  640.             break;
  641.         case 2:
  642.             commit = AFPhoneCommitRegExp;
  643.             noCommit = AFPhoneEntryRegExp;
  644.             break;
  645.         case 3:
  646.             commit = AFSSNCommitRegExp;
  647.             noCommit = AFSSNEntryRegExp;
  648.             break;
  649.     }        
  650.     if(!AFExactMatch(event.willCommit ? commit : noCommit, value))
  651.     {
  652.         app.beep(0);
  653.         event.rc = false;
  654.     }
  655. }
  656.  
  657. function AFDate_Keystroke(pdf)
  658. {       /* This function validates the current keystroke event to make sure the
  659.         key pressed is reasonable for a date field. */
  660.  
  661.         if(event.willCommit && !AFParseDateWithPDF(AFMergeChange(event), pdf))
  662.                         /* dates are only validated on commit */
  663.         {
  664.             app.beep(0);
  665.             event.rc = false;
  666.         }
  667. }
  668.  
  669. function AFTime_Keystroke(ptf)
  670. {       /* This function validates the current keystroke event to make sure the
  671.         key pressed is reasonable for a time field. */
  672.  
  673.         if(event.willCommit && !AFParseTime(event.value, null))
  674.                         /* times are only validated on commit */
  675.         {
  676.             app.beep(0);
  677.             event.rc = false;
  678.         }
  679. }
  680.  
  681. function AFNumber_Format(nDec, sepStyle, negStyle, currStyle, strCurrency, bCurrencyPrepend)
  682. {       /* This function formats a numeric value according to the parameters. */
  683.  
  684.     var value = AFMakeNumber(event.value);
  685.     var sign = (value < 0 ? -1 : 1);
  686.     var f = event.target;
  687.  
  688.     if(value == null)
  689.     {
  690.         event.value = "";
  691.         return;
  692.     }    
  693.     if ((negStyle == 2 /* ParensBlack */ || negStyle == 3 /* ParensRed */) && value < 0)
  694.         var formatStr = "(";
  695.     else 
  696.         var formatStr = "";
  697.     
  698.     if (bCurrencyPrepend)
  699.         formatStr = formatStr + strCurrency;
  700.         
  701.     formatStr = formatStr + "%," + sepStyle + "." + nDec + "f";
  702.     if (! bCurrencyPrepend)
  703.         formatStr = formatStr + strCurrency;
  704.         
  705.     if ((negStyle == 2 /* ParensBlack */ || negStyle == 3 /* ParensRed */) && value < 0)
  706.         formatStr = formatStr + ")";
  707.  
  708.     if (negStyle != 0 /* MinusBlack */ || bCurrencyPrepend)
  709.         value = Math.abs(value);
  710.         
  711.     if (sign > 0 )
  712.         f.fgColor = color.black;
  713.     else if (negStyle == 1 /* Red */ || negStyle == 3 /* ParensRed */)
  714.         f.fgColor = color.red;
  715.     else
  716.         f.fgColor = color.black;
  717.     
  718.     var tmp = util.printf(formatStr, value);
  719.     if (sign < 0 && bCurrencyPrepend && negStyle == 0)
  720.         tmp = '-' + tmp; /* prepend the -ve sign */
  721.     event.value = tmp;
  722. }
  723.  
  724. function AFPercent_Format(nDec, sepStyle)
  725. {       /* This function formats a percentage value according to the parameters. */
  726.  
  727.     var value = AFMakeNumber(event.value) * 100;
  728.     
  729.     var formatStr = "%," + sepStyle + "." + nDec + "f";
  730.         
  731.     if(value == null)
  732.     {
  733.         event.value = "";
  734.         return;
  735.     }    
  736.  
  737.     value = util.printf(formatStr, value);
  738.     
  739.     event.value = value + "%";
  740. }
  741.  
  742. function AFSpecial_Format(psf)
  743. {   /* This function formats a "special" value according to the "PropsSpecialFormat" parameter psf. */
  744.     /* The special formats, indicated by psf, are: 0 = zip code, 1 = zip + 4, 2 = phone, 3 = SSN. */
  745.     var value = event.value;
  746.  
  747.     if(!value) return;    
  748.     switch (psf) {
  749.     
  750.         case 0:                         
  751.             var formatStr = "99999";
  752.             break;
  753.         case 1:                         
  754.             var formatStr = "99999-9999";
  755.             break;
  756.         case 2:                         /* must distinguish between 2 styles: with and without area code */
  757.             var NumbersStr = util.printx("9999999999", value);      /* try to suck out 10 numeric chars */
  758.             if (NumbersStr.length >= 10 )
  759.                 var formatStr = "(999) 999-9999";
  760.             else 
  761.                 var formatStr = "999-9999";
  762.             break;
  763.         case 3:
  764.             var formatStr = "999-99-9999";
  765.             break;
  766.     }
  767.         
  768.     event.value = util.printx(formatStr, value);
  769. }
  770.  
  771. function AFDate_Format(pdf)
  772. {       /* This function formats a date value according to the "PropsDateFormat" parameter pdf. */
  773.     /* The date formats, indicated by pdf, are:
  774.     
  775.     pdf             format                                                          
  776.     ---             ------                                                          
  777.     0               PDF_M_D         [ 1/3                           ]       
  778.     1               PDF_M_D_YY      [ 1/3/81                        ]       
  779.     2               PDF_MM_DD_YY    [ 01/03/81                      ]       
  780.     3               PDF_MM_YY       [ 03/81                         ]
  781.     4               PDF_D_MMM,      [ 3-Jan                         ]       
  782.     5               PDF_D_MMM_YY    [ 3-Jan-81                      ]       
  783.     6               PDF_DD_MMM_YY   [ 03-Jan-81                     ]       
  784.     7               PDF_YY_MM_DD    [ 81-01-03                      ]
  785.     8               PDF_MMM_YY      [ Jan-81                        ]       
  786.     9               PDF_MMMM_YY     [ January-81            ]       
  787.     10              PDF_MMM_D_YYYY  [ Jan 3, 1981           ]
  788.     11              PDF_MMMM_D_YYYY [ January 3, 1981       ]       
  789.     12              PDF_M_D_YY_12HR [ 1/3/81 2:30 PM        ]       
  790.     13              PDF_M_D_YY_24HR [ 1/3/81 14:30          ]       
  791.  
  792.     */
  793.  
  794.     var date = AFParseDateWithPDF(event.value, pdf);
  795.     var formatStr;
  796.     
  797.     if(!event.value) return;    /* Blank fields remain blank */
  798.     if(!date)
  799.     {
  800.         event.value = "";
  801.         return;
  802.     }
  803.     
  804.     switch (pdf) {
  805.     
  806.         case 0:
  807.             formatStr = "m/d";
  808.             break;
  809.         case 1:
  810.             formatStr = "m/d/yy";
  811.             break;
  812.         case 2:
  813.             formatStr = "mm/dd/yy";
  814.             break;
  815.         case 3:
  816.             formatStr = "mm/yy";
  817.             break;
  818.         case 4:
  819.             formatStr = "d-mmm";
  820.             break;
  821.         case 5:
  822.             formatStr = "d-mmm-yy";
  823.             break;
  824.         case 6:
  825.             formatStr = "dd-mmm-yy";
  826.             break;
  827.         case 7:
  828.             formatStr = "yy-mm-dd";
  829.             break;
  830.         case 8:
  831.             formatStr = "mmm-yy";
  832.             break;
  833.         case 9:
  834.             formatStr = "mmmm-yy";
  835.             break;
  836.         case 10:
  837.             formatStr = "mmm d, yyyy";
  838.             break;
  839.         case 11:
  840.             formatStr = "mmmm d, yyyy";
  841.             break;
  842.         case 12:
  843.             formatStr = "m/d/yy h:MM tt";
  844.             break;
  845.         case 13:
  846.             formatStr = "m/d/yy HH:MM";
  847.             break;
  848.     }
  849.     
  850.     event.value = util.printd(formatStr, date);
  851. }
  852.  
  853. function AFTime_Format(ptf)
  854. {       /* This function formats a time value according to the "PropsTimeFormat" parameter ptf. */
  855.     /* The time formats, indicated by ptf, are:
  856.     
  857.     ptf             format                                                          
  858.     ---             ------                                                          
  859.     0               PTF_24HR_MM     [ 14:30                         ]
  860.     1               PTF_12HR_MM     [ 2:30 PM                       ]
  861.     2               PTF_24HR_MM_SS  [ 14:30:15                      ]
  862.     3               PTF_12HR_MM_SS  [ 2:30:15 PM            ]
  863.  
  864.     */
  865.  
  866.     var date = new AFParseTime(event.value, null);
  867.     var formatStr;
  868.     
  869.     if(!event.value) return;    /* Blank fields remain blank */
  870.     if(!date) {
  871.         event.value = "";
  872.         return;
  873.     }
  874.  
  875.     switch (ptf) {
  876.     
  877.         case 0:
  878.             formatStr = "HH:MM";
  879.             break;
  880.         case 1:
  881.             formatStr = "h:MM tt";
  882.             break;
  883.         case 2:
  884.             formatStr = "HH:MM:ss";
  885.             break;
  886.         case 3:
  887.             formatStr = "h:MM:ss tt";
  888.             break;
  889.     }
  890.     
  891.     event.value = util.printd(formatStr, date);
  892. }
  893.